//
//  MSTXViewController.m
//  FruitsWholesaler
//
//  Created by sgone-2 on 15/12/2.
//  Copyright © 2015年 sgone. All rights reserved.
//

#import "MSTXViewController.h"
#import "AFNetworking.h"
#import "MBProgressHUD+MJ.h"
@interface MSTXViewController ()<UITableViewDataSource,UITableViewDelegate,UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tableView;


@property(nonatomic,strong)  AFHTTPSessionManager *manager;
@property(nonatomic,strong) NSMutableDictionary *list;

@property(nonatomic,strong) NSString *existing;
@property(nonatomic,strong) NSString *could_apply;
@property(nonatomic,strong) NSString *has_apply;


@property(nonatomic,strong) NSString *bank_name;
@property(nonatomic,strong) NSString *payee_name;
@property(nonatomic,strong) NSString *bank_account;


@property(nonatomic,strong) UILabel *cellLbl;



//提现金额
@property(nonatomic,strong) UITextField *textTxje;
//全局变量进行记住内容
@property(nonatomic,strong) NSString *textTxjeN;
@property(nonatomic,strong) UIView *mengBanView;


//提现密码
@property(nonatomic,strong) UITextField *TextTxmm;
//全局变量进行记住内容
@property(nonatomic,strong) NSString *TextTxmmN;




@end

@implementation MSTXViewController



//afnetworking网络请求数据
- (AFHTTPSessionManager *)manager
{
    if (_manager == nil) {
        _manager  = [AFHTTPSessionManager manager];
    }
    return _manager;
    
    
}



- (void)viewDidLoad {
    [super viewDidLoad];

//    self.title = @"马上提现";
    self.titleLabel.text = @"马上提现";
    self.backBtn.hidden = NO;
    self.list = [NSMutableDictionary dictionaryWithCapacity:0];
    
    
    [self sendAPI];
    
    



}



#pragma mark 获取网络数据
-(void) sendAPI
{

    NSString *urlStr = [NSString stringWithFormat:@"%@get_account",SERVER_ADDRESS2];

    NSDictionary *parmas = @{@"wTel" : self.dataHandler.phone ,@"uToken" : self.dataHandler.uToken};
    
    [MBProgressHUD showHUDAddedTo:self.tableView animated:YES];
    
    [self.manager POST:urlStr parameters:parmas success:^(NSURLSessionDataTask *task, id responseObject) {
        
        if ([responseObject[@"error_flag"] intValue] == 0) {
            
            
            NSDictionary *dict  = responseObject[@"data"][@"account_info"];
            
            
//            NSLog(@"wttwtwtwtt %@",dict);
            self.existing = [NSString stringWithFormat:@"￥%@",[dict objectForKey:@"existing"]];
            self.could_apply = [NSString stringWithFormat:@"￥%@",[dict objectForKey:@"could_apply"]];
            self.has_apply = [NSString stringWithFormat:@"￥%@",[dict objectForKey:@"has_apply"]];
            self.bank_name = [NSString stringWithFormat:@"%@",[dict objectForKey:@"bank_name"]];
            self.payee_name = [NSString stringWithFormat:@"%@",[dict objectForKey:@"payee_name"]];
            self.bank_account = [NSString stringWithFormat:@"%@",[dict objectForKey:@"bank_account"]];
            
            
        
            
//            NSLog(@"hhhhhhhh  %@",dict);
            [self.tableView reloadData];
            [MBProgressHUD hideAllHUDsForView:self.tableView animated:YES];

        }
        else
        {
            
            [MBProgressHUD hideAllHUDsForView:self.tableView animated:YES];
            NSString *str = [NSString stringWithFormat:@"%@",responseObject[@"result_msg"]];
            UIAlertView *alter = [[UIAlertView alloc]initWithTitle:@"提示" message:str delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
            [alter show];

        
        
        }
        
        
        
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
        
        [MBProgressHUD hideAllHUDsForView:self.tableView animated:YES];

        UIAlertView *alter = [[UIAlertView alloc]initWithTitle:@"提示" message:@"网络获取失败" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
        
        [alter show];
        
        
    }];
    
    



}



-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{

    return 5;

}



-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{


    if (section == 0) {
        
        return 3;
    }
    else if (section == 1)
    {
    
        return 1;
    
    
    }
    else if (section==2)
    {
    
        return 3;
    
    }
    else if (section == 3)
    {
    
        return 1;
    
    }
    else
    {
    
        return 1;
    
    }
    
    


}




-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{

    
    
    if (indexPath.section == 3) {
        
        
        return 70;
        
    }
    else
    {
    
    return 50;
    }

}







-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    
    
    if (section == 0) {
        
        return 15;
        
    }
    else if (section == 1)
    {
        
        return 15;
        
    }
    else if (section == 2)
    {
        
        return 30;
        
    }
    else if (section == 3)
    {
        
        return 30;
    }
    else
    {
    
        return 15;
    
    }
}






-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    
    
    UIView *view2 = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 10, 10)];
    [self.view addSubview:view2];
    
    if (section == 2)
    {
        
        UILabel *lbl = [[UILabel alloc]initWithFrame:CGRectMake(15, 0, 200, 30)];
        lbl.text = @"账户信息";
        lbl.font = [UIFont systemFontOfSize:15];
        lbl.textColor = [UIColor grayColor];
        [view2 addSubview:lbl];
    }
    else if (section == 3)
    {
    
        
        UILabel *lbl = [[UILabel alloc]initWithFrame:CGRectMake(15, 0, 200, 30)];
        lbl.text = @"提现规则";
        lbl.font = [UIFont systemFontOfSize:15];
        lbl.textColor = [UIColor grayColor];
        [view2 addSubview:lbl];

    
    
    }
    
    
    
    return view2;
    
    
    
}




-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{


    
    if (section == 4)
    {
        return 90;
    }
    
    else
    {
    return 0.01;
    }

}


-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{

    
    UIView *view10 = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 10, 10)];
    [self.view addSubview:view10];
    
    if (section == 4) {
     
    
    
    UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(0, 30, ViewWidth, 60)];
    btn.backgroundColor = BenSe_COLOR;
    [view10 addSubview:btn];
      
        btn.titleLabel.font = [UIFont systemFontOfSize:18];
    [btn setTitle:@"马 上 提 现" forState:UIControlStateNormal];
    
    [btn addTarget:self action:@selector(sure:) forControlEvents:UIControlEventTouchUpInside];

    }
    
    return view10;

}



#pragma mark提交密码进行提现操作
-(void)sure:(id)sender
{
    
    [self.textTxje resignFirstResponder];
    [self.TextTxmm resignFirstResponder];
    
    
    
    if (self.textTxje.text.length == 0) {
        
        [self.view makeToast:@"请输入提现金额"];
        return;
    }
    if (self.TextTxmm.text.length == 0) {
        
        [self.view makeToast:@"请输入密码"];
        return;
    }
    
    if ([self.textTxje.text floatValue] < 100.00) {
        
        [self.view makeToast:@"提现金额少于100"];
        return;
    }
    
    //判断提现金额是否大于余额，可以省略后台可以提示控制
//    if ([self.textTxje.text floatValue] ) {
//        <#statements#>
//    }
    
    
    
    NSString *MM = (md5) (self.TextTxmm.text);
    
    NSString *urlString = [NSString stringWithFormat:@"%@add_wholesaler_io_record",SERVER_ADDRESS2];
    NSDictionary *pramas = @{@"wTel" : self.dataHandler.phone , @"uToken" : self.dataHandler.uToken , @"amount" : self.textTxje.text , @"apply_passwd" : MM};
    
    
    
    [MBProgressHUD showHUDAddedTo:self.tableView animated:YES];
    [self.manager POST:urlString parameters:pramas success:^(NSURLSessionDataTask *task, id responseObject) {
        
        if ([responseObject[@"error_flag"] intValue] == 0) {
        
//            NSLog(@"上传到服务器成功  %@",responseObject);
            
            
            [MBProgressHUD hideAllHUDsForView:self.tableView animated:YES];
            
            
            UIAlertView *alter = [[UIAlertView alloc]initWithTitle:@"提示" message:@"提现成功" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
            [alter show];
            [self.navigationController popViewControllerAnimated:YES];
            
        }
        
        
        else
        {
        
            
            [MBProgressHUD hideAllHUDsForView:self.tableView animated:YES];
            NSString *str = [NSString stringWithFormat:@"%@",[responseObject objectForKey:@"result_msg"]];
            UIAlertView *alter  = [[UIAlertView alloc]initWithTitle:@"提示" message:str delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
            [alter show];
            
        
        
        }
        
        
        
        
        
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
        [MBProgressHUD hideAllHUDsForView:self.tableView animated:YES];
       
        UIAlertView *alter  = [[UIAlertView alloc]initWithTitle:@"提示" message:@"连接服务器失败" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
        [alter show];
        
    }];
    
    
    





}




-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    static NSString *CellIdentifier = @"Cell";
    
         UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        cell.selectionStyle= UITableViewCellSelectionStyleNone;
        cell.textLabel.textColor = [UIColor grayColor];
        cell.textLabel.font = [UIFont systemFontOfSize:16];
        
        
        UILabel *lbl = [[UILabel alloc]initWithFrame:CGRectMake(ViewWidth *0.5, 0, ViewWidth *0.47, 50)];
        lbl.textColor = [UIColor lightGrayColor];
        lbl.font = [UIFont systemFontOfSize:15];
        lbl.textAlignment = NSTextAlignmentRight;
        [cell addSubview:lbl];
        lbl.tag =110;
    
    
    
   
    
    

    
    
    
    
    
    if (indexPath.section == 0) {
        
        if (indexPath.row == 0) {
            
            cell.textLabel.text = @"当前余额";
            lbl.text = [NSString stringWithFormat:@"%@元",self.existing];
            lbl.textColor = BenSe_COLOR;
            
            
        }
        
        else if (indexPath.row == 1)
        {
        
        cell.textLabel.text = @"可提余额";
            lbl.text = [NSString stringWithFormat:@"%@元",self.could_apply];
            lbl.textColor = BenSe_COLOR;
        
        }
        else if (indexPath.row == 2)
        {
        cell.textLabel.text = @"本期已申请";
        
            lbl.text = [NSString stringWithFormat:@"%@元",self.has_apply];
            lbl.textColor = BenSe_COLOR;
        
        
        }
        else
        {
        
            

        
        
        }
        
        
        
        
        
    }
    else if (indexPath.section == 1)
    {
    
        if (indexPath.row == 0) {
            
            cell.textLabel.text = @"输入提现金额";
            self.textTxje = [[UITextField alloc]initWithFrame:CGRectMake(ViewWidth *0.45, 0, ViewWidth *0.45, 50)];
            self.textTxje.textColor = [UIColor lightGrayColor];
            self.textTxje.font = [UIFont systemFontOfSize:15];
            self.textTxje.textAlignment = NSTextAlignmentRight;
            [cell addSubview:self.textTxje];
            self.textTxje.delegate = self;
            self.textTxje.returnKeyType = UIReturnKeyDone;
            self.textTxje.placeholder = @"请点击输入提现金额";
            self.textTxje.keyboardType = UIKeyboardTypeDecimalPad;
            
            self.textTxje.tag = 11;
            
            self.textTxje.text = self.textTxjeN;
            
            
            
            UILabel *lbl2 = [[UILabel alloc]initWithFrame:CGRectMake(ViewWidth *0.93, 0, 0.07 *ViewWidth, 50)];
            lbl2.text = @"元";
            lbl2.font = [UIFont systemFontOfSize:16];
            lbl2.textColor = [UIColor grayColor];
            [cell addSubview:lbl2];
            
            
            
            
        }
    
    
    }
    
    else if (indexPath.section == 2)
    {
        
        
        

    
        if (indexPath.row == 0) {
            cell.textLabel.text = @"开户银行";
            lbl.text = self.bank_name;
        }
        else if (indexPath.row == 1)
        {
        
        cell.textLabel.text = @"收款户名";
            lbl.text = self.payee_name;
        
        }
        else if (indexPath.row == 2)
        {
        cell.textLabel.text = @"收款账户";
        lbl.text = self.bank_account;
        lbl.font = [UIFont systemFontOfSize:14];
        
        
        }
            
    
    
    
    
    }
    
    else if (indexPath.section == 3)
   {

    
       UITextView *textView = [[UITextView alloc]initWithFrame:CGRectMake(10, 0, ViewWidth-30, 70)];
       textView.text = @"1.每户商家每自然月可提现4次。\n2.每次提现金额不小于100元。\n3.提现后5个工作日内完成打款。";
       textView.textColor = [UIColor grayColor];
       textView.font = [UIFont systemFontOfSize:15];
       textView.editable = NO;
       [cell addSubview:textView];

    }
  else if(indexPath.section == 4)
  {
  
      self.TextTxmm = [[UITextField alloc]initWithFrame:CGRectMake(15, 0, ViewWidth-15, 50)];
      [cell addSubview:self.TextTxmm];
      self.TextTxmm.placeholder = @"输入提现密码";
      self.TextTxmm.font = [UIFont systemFontOfSize:16];
      self.TextTxmm.textColor = [UIColor grayColor];
      self.TextTxmm.delegate = self;
      self.TextTxmm.returnKeyType = UIReturnKeyDone;
      self.TextTxmm.secureTextEntry = YES;
      self.TextTxmm.tag = 12;
      self.TextTxmm.text = self.TextTxmmN;
  
  }
    
    
    
    
    
    



    return cell;


}



-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{


    
    if (textField == self.textTxje) {
        
        
        
        if (self.mengBanView == nil) {
      
        
        //蒙版
        self.mengBanView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height -64)];
        self.mengBanView.alpha = 0.2;
            
        self.mengBanView.backgroundColor = [UIColor whiteColor];
        [self.tableView addSubview:self.mengBanView];
        
        

        }
        
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(Tapped6:)];
        
        [self.mengBanView addGestureRecognizer:tap];
        
        
    }
    
    
    
    
    return YES;
    


}


#pragma mark 如果没有没有doneButton的产生就收回就点击空白收回键盘
-(void)Tapped6:(UITapGestureRecognizer*)gestureRecognizer
{
    
    [self.mengBanView removeFromSuperview];
    
    self.mengBanView = nil;

    
    [[[UIApplication sharedApplication] keyWindow] endEditing:YES];//关闭键盘
    
    
    
}



 



-(void)textFieldDidBeginEditing:(UITextField *)textField
{
    
    
        
        
        if (textField == self.TextTxmm) {
            
            
            
            [UIView animateWithDuration:0.25 animations:^{
                if (self.view.y == 0) {
                    self.view.y = -130;
                }
            }completion:^(BOOL finished){
                //生成pickView
                
                
                
            }];
            
        }
    
}



#pragma mark textField进行end操作记住内容
-(void)textFieldDidEndEditing:(UITextField *)textField

{

    if (textField.tag == 11) {
        
        
        self.textTxjeN = self.textTxje.text;
        
        
    }
   else if (textField.tag == 12)
   {


    self.TextTxmmN = self.TextTxmm.text;

    }



}


#pragma mark 收回键盘操作
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    
    [textField resignFirstResponder];
    
    
    [UIView animateWithDuration:0.25 animations:^{
        if (self.view.y == -130) {
            self.view.y = 0;
        }
    }completion:^(BOOL finished){
        //生成pickView
        
        
        
    }];
    
    return YES;
    
    
    
}


@end
